﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
using System.Xml.Serialization;

using n = EnnErrE.Utils.Numbers;

namespace EnnErrE.BillFiller
{
    /// <summary>
    /// Definizione di un bollettino.
    /// </summary>
    /// <remarks>Le dimensioni sono espresse in millimetri.</remarks>
    public class BillSettings : IComparable
    {
        /// Restituisce le dimensioni per un bollettino Mod. Ch8 ter (32 x 10 cm).
        /// </summary>
        public static PaperSize Ch8Ter
        {
            get
            {
                PaperSize result = new PaperSize("Mod. Ch8 ter", (int)Math.Round(n.Convert.mm2inch(320) * 100), (int)Math.Round(n.Convert.mm2inch(100) * 100));
                result.RawKind = (int)PaperKind.Custom;
                return result;
            }
        }
        /// <summary>
        /// Restituisce le dimensioni per un bollettino postale Mod. Ch8 BIS (30 x 10 cm).
        /// </summary>
        public static PaperSize Ch8Bis
        {
            get
            {
                PaperSize result = new PaperSize("Mod. Ch8 BIS", (int)Math.Round(n.Convert.mm2inch(298) * 100), (int)Math.Round(n.Convert.mm2inch(102) * 100));
                result.RawKind = (int)PaperKind.Custom;
                return result;
            }
        }
        /// <summary>
        /// Bollettino postale "Mod. Ch 8 BIS". Bollettino con cedola di ricevuta di versamento e ricevuta di accredito. Dimensioni 30 x 10 cm.
        /// </summary>
        public static BillSettings Mod12401A
        {
            get
            {
                BillSettings result = new BillSettings("Mod. Ch8 BIS / 12401A", Ch8Bis);
                // ***** Versamento *****
                // C/C
                result.fields.Add(new BillBoxedText("accountNumber", new PointF(22, 13), new SizeF(4, 5), 12));
                // Euro
                result.fields.Add(new BillNumber("amount", new PointF(83, 13), new SizeF(4, 5), 10, 2, 1));
                // Importo in lettere
                result.fields.Add(new BillTextLine("literalizedAmount", new PointF(32, 25), new SizeF(94, 8), SizeMode.Trim));
                // Intestatario
                result.fields.Add(new BillTextLine("company", new PointF(11, 33), new SizeF(115, 5), SizeMode.Trim));
                // Causale
                result.fields.Add(new BillTextLine("motivation", new PointF(11, 46), new SizeF(115, 5), SizeMode.Trim));
                // Autore del versamento
                result.fields.Add(new BillTextLine("author", new PointF(11, 60), new SizeF(60, 5), SizeMode.Trim));
                // Via
                result.fields.Add(new BillTextLine("address", new PointF(22, 72), new SizeF(50, 5), SizeMode.Trim));
                // CAP
                result.fields.Add(new BillTextLine("zip", new PointF(20, 79), new SizeF(50, 5), SizeMode.Trim));
                // Localita'
                result.fields.Add(new BillTextLine("city", new PointF(20, 85), new SizeF(50, 5), SizeMode.Trim));
                // ***** Accredito *****
                // C/C 
                result.fields.Add(new BillBoxedText("accountNumber", new PointF(164, 13), new SizeF(4, 5), 12));
                // Euro
                result.fields.Add(new BillNumber("amount", new PointF(248, 13), new SizeF(4, 5), 10, 2, 1));
                // Importo in lettere 
                result.fields.Add(new BillTextLine("literalizedAmount", new PointF(184, 24), new SizeF(105, 8), SizeMode.Trim));
                // Intestatario
                result.fields.Add(new BillBoxedText("company", new PointF(138, 28), new SizeF(4, 5), 34, 5.5f));
                // Causale
                result.fields.Add(new BillTextLine("motivation", new PointF(138, 45), new SizeF(147, 5), SizeMode.Trim));
                // Autore del versamento
                result.fields.Add(new BillBoxedText("author", new PointF(186, 56), new SizeF(4, 5), 23, 5.5f));
                // Via
                result.fields.Add(new BillBoxedText("address", new PointF(186, 69), new SizeF(4, 5), 23, 0));
                // CAP
                result.fields.Add(new BillBoxedText("zip", new PointF(186, 78), new SizeF(4, 5), 5, 0));
                // Localita'
                result.fields.Add(new BillBoxedText("city", new PointF(213, 78), new SizeF(4, 5), 17, 0));
                return result;
            }
        }
        /// <summary>
        /// Bollettino postale "Mod. Ch 8 ter". Bollettino con cedola di attestazione di versamento, ricevuta di versamento e ricevuta di accredito. Dimensioni 32 x 10 cm.
        /// </summary>
        public static BillSettings Mod12409A
        {
            get
            {
                BillSettings result = new BillSettings("Mod. Ch8 ter / 12409A", Ch8Ter);
                // ***** Attestazione di Versamento *****
                // C/C
                result.fields.Add(new BillBoxedText("accountNumber", new PointF(23.5f, 8.5f), new SizeF(4, 5), 12));
                // Euro
                result.fields.Add(new BillNumber("amount", new PointF(23.5f, 18), new SizeF(4, 5), 10, 2, 1));
                // Importo in lettere
                result.fields.Add(new BillTextLine("literalizedAmount", new PointF(15, 29), new SizeF(62, 8), SizeMode.Trim));
                // Intestatario
                result.fields.Add(new BillTextLine("company", new PointF(15, 33), new SizeF(62, 5), SizeMode.Trim));
                // Causale
                result.fields.Add(new BillTextLine("motivation", new PointF(4, 42), new SizeF(74, 5), SizeMode.Trim));
                // Autore del versamento
                result.fields.Add(new BillTextLine("author", new PointF(17, 87), new SizeF(60, 5), SizeMode.Trim));
                // Via
                result.fields.Add(new BillTextLine("address", new PointF(17, 93), new SizeF(60, 5), SizeMode.Trim));
                // CAP
                result.fields.Add(new BillTextLine("zip", new PointF(8, 99), new SizeF(13, 5), SizeMode.Trim));
                // Localita'
                result.fields.Add(new BillTextLine("city", new PointF(33, 99), new SizeF(40, 5), SizeMode.Trim));
                // ***** Ricevuta di Versamento *****
                // C/C
                result.fields.Add(new BillBoxedText("accountNumber", new PointF(117, 8.5f), new SizeF(4, 5), 12));
                // Euro
                result.fields.Add(new BillNumber("amount", new PointF(117, 18), new SizeF(4, 5), 10, 2, 1));
                // Importo in lettere
                result.fields.Add(new BillTextLine("literalizedAmount", new PointF(98, 29), new SizeF(62, 8), SizeMode.Trim));
                // Intestatario
                result.fields.Add(new BillTextLine("company", new PointF(98, 33), new SizeF(62, 5), SizeMode.Trim));
                // Causale
                result.fields.Add(new BillTextLine("motivation", new PointF(87, 42), new SizeF(74, 5), SizeMode.Trim));
                // Autore del versamento
                result.fields.Add(new BillTextLine("author", new PointF(100, 87), new SizeF(60, 5), SizeMode.Trim));
                // Via
                result.fields.Add(new BillTextLine("address", new PointF(100, 93), new SizeF(60, 5), SizeMode.Trim));
                // CAP
                result.fields.Add(new BillTextLine("zip", new PointF(90, 99), new SizeF(13, 5), SizeMode.Trim));
                // Localita'
                result.fields.Add(new BillTextLine("city", new PointF(120, 99), new SizeF(40, 5), SizeMode.Trim));
                // ***** Ricevuta di Accredito *****
                // C/C
                result.fields.Add(new BillBoxedText("accountNumber", new PointF(20, 8.5f), new SizeF(4, 5), 12));
                // Euro
                result.fields.Add(new BillNumber("amount", new PointF(28, 8.5f), new SizeF(4, 5), 10, 2, 1));
                // Importo in lettere
                result.fields.Add(new BillTextLine("literalizedAmount", new PointF(220, 20), new SizeF(62, 8), SizeMode.Trim));
                // Intestatario
                result.fields.Add(new BillBoxedText("company", new PointF(174, 28), new SizeF(4, 5), 34, 5.5f));
                // Causale
                result.fields.Add(new BillTextLine("motivation", new PointF(175, 42), new SizeF(60, 5), SizeMode.Trim));
                // Autore del versamento
                result.fields.Add(new BillBoxedText("author", new PointF(223, 51), new SizeF(4, 5), 23,5.5f));
                // Via
                result.fields.Add(new BillBoxedText("address", new PointF(223, 64f), new SizeF(4, 5), 23));
                // CAP
                result.fields.Add(new BillBoxedText("zip", new PointF(223, 73), new SizeF(4, 5), 5));
                // Localita'
                result.fields.Add(new BillBoxedText("city", new PointF(248, 73), new SizeF(4, 5), 17));

                return result;
            }
        }
        /// <summary>
        /// Memorizza l'orientamento del bollettino.
        /// </summary>
        private bool landscape;
        /// <summary>
        /// Memorizza il nome del bollettino.
        /// </summary>
        private string name;
        /// <summary>
        /// Memorizza le dimensioni del bollettino.
        /// </summary>
        private PaperSize billSize;
        /// <summary>
        /// Memorizza il font utilizzato per la compilazione del bollettino.
        /// </summary>
        private Font font;
        /// <summary>
        /// Memorizza i campi del bollettino.
        /// </summary>
        private List<BillField> fields;
        /// <summary>
        /// Imposta/Restituisce l'orientamento del bollettino.
        /// </summary>
        public bool Landscape { get { return landscape; } set { landscape = value; } }
        /// <summary>
        /// Imposta/Restituisce il nome del bollettino.
        /// </summary>
        public string Name { get { return name; } set { name = value; } }
        /// <summary>
        /// Imposta/Restituisce le dimensioni del bollettino.
        /// </summary>
        public PaperSize BillSize { get { return billSize; } set { billSize = value; } }
        /// <summary>
        /// Imposta/Restituisce il font utilizzato per la compilazione del bollettino.
        /// </summary>
        public Font Font { get { return font; } set { font = value; } }
        /// <summary>
        /// Imposta/Restituisce i campi del bollettino.
        /// </summary>
        public List<BillField> Fields { get { return fields; } set { fields = value; } }
        /// <summary>
        /// Costruttore di default.
        /// </summary>
        /// <remarks>Crea le impostazioni per un bollettino <see  cref="BillSettings.Ch8Ter"/>.</remarks>
        public BillSettings() : this("Mod. Ch8 ter", Ch8Ter) { }
        /// <summary>
        /// Costruttore.
        /// </summary>
        /// <param name="name">Nome assegnato al bollettino.</param>
        /// <param name="billSize">Dimensioni del bollettino.</param>
        public BillSettings(string name, PaperSize billSize)
        {
            Name = name;
            BillSize = billSize;
            Font = new Font(FontFamily.GenericMonospace, 10f);
            Fields = new List<BillField>();
        }
        /// <summary>
        /// Fornisce la rappresentazione come stringa dell'oggetto.
        /// </summary>
        /// <returns>Il nome associato al bollettino.</returns>
        public override string ToString()
        {
            return Name;
        }
        /// <summary>
        /// Confronta con un altro oggetto.
        /// </summary>
        /// <param name="obj">Oggeto con cui operare il confronto.</param>
        /// <returns>Risultato del confronto.</returns>
        public int CompareTo(object obj)
        {
            if (obj is BillSettings)
                return Name.CompareTo((obj as BillSettings).Name);
            else
                return 1;
        }
        /// <summary>
        /// Rileva l'uguaglianza con un altro oggetto.
        /// </summary>
        /// <param name="obj">Oggetto con cui operare il confronto.</param>
        /// <returns>Risultato del confronto.</returns>
        public override bool Equals(object obj)
        {
            return CompareTo(obj) == 0;
        }
        /// <summary>
        /// Calcola il codice hash associato all'oggetto.
        /// </summary>
        /// <returns>Codice hash associato all'oggetto.</returns>
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }
}
